package com.git.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.git.annotation.GetUser;
import com.git.annotation.RequireAuth;
import com.git.domain.AjaxJson;
import com.git.domain.User;
import com.git.service.UserService;
import com.git.utils.JWTUtils;

@Controller
public class DemoController {
	@Autowired
	private UserService demoService;
	
	@RequestMapping("login")
	public ResponseEntity<AjaxJson> login(User user){
		User userDB = this.demoService.checkUser(user);
		if (userDB==null) {
			return ResponseEntity.status(200).body(new AjaxJson(true,"false","用户名或密码不对"));
		}
		String jwt = JWTUtils.getJWT(userDB.getId()+"");
		return ResponseEntity.status(200).body(new AjaxJson(true,"success",jwt));
	}
	
	
	/**
	 * 如果带来的JWT是有效的，这里的User是有值的
	 * @param user
	 * @return
	 */
	@RequestMapping("get")
	@RequireAuth
	public ResponseEntity<AjaxJson> get(@GetUser User user){
		return ResponseEntity.status(200).body(new AjaxJson(true,"success",user));
	}
	
}
